﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Card.Manager
{
    public partial class LogoManager : BasePage
    {
        #region
        private int Records
        {
            get
            {
                int _Records = 50;
                if (int.TryParse(this.txtRecords.Text, out _Records) && _Records > 0)
                {
                    return _Records;
                }
                else
                {
                    this.txtRecords.Text = "50";
                    return 50;
                }
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        protected override void OnInit(EventArgs e)
        {
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            this.pager.BuildingUrl += new BAF.WebControls.BuildingUrlHander(pager_BuildingUrl);
            base.OnInit(e);
        }

        string pager_BuildingUrl(int index)
        {
            return string.Format("LogoManager.aspx?i={0}", index);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName.ToString(), "DELETE") == 0)
            {
                int id = 0;
                if (int.TryParse(e.CommandArgument.ToString(), out id))
                {
                    if (BLL.LogoManager.Delete(id, null))
                    {
                        this.MessageBox("删除成功！");
                        this.DoQuery();
                    }
                }
            }
        }

        #endregion

        #region 方法
        private void DoInit()
        {
            List<BAF.Entity.LookupValue> lstType = Card.Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.LOGO_TYPE);
            Card.Utility.ControlUtility.BindListControl<BAF.Entity.LookupValue>(this.dropType, lstType, "Value", "LookupCode", true);
            this.DoQuery();
        }

        private void DoQuery()
        {
            
            Card.DataBase.Pagination pagination = new Card.DataBase.Pagination()
            {
                TableName = "LOGO",
                PageSize = this.pager.PageSize,
                OrderBy = "CREATE_DATE DESC,ID",
                PageNumber = this.pager.PageIndex + 1,
                Condition = GetCondition()
            };

            DataTable dtData = BLL.TemplateManager.Query(pagination);
            this.rptList.DataSource = dtData;
            this.pager.RecordCount = pagination.RecordCount;
            this.rptList.DataBind();
        }

        private string GetCondition()
        {
            StringBuilder sb = new StringBuilder(" AND NAME LIKE '%" + this.txtName.Text + "%'  ");
            if (!string.IsNullOrEmpty(this.dropType.SelectedValue))
            {
                sb.Append(string.Format(" AND TYPE_CODE='{0}'", this.dropType.SelectedValue));
            }

           
            return sb.ToString();
        }
        #endregion

        
    }
}
